package com.ndp.fb.rdb.db.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ndp.fb.rdb.api.AdCampaignRdbService;
import com.ndp.fb.rdb.db.DaoTemplate;
import com.ndp.fb.rdb.db.impl.mapper.DefaultRowMapper;
import com.ndp.fb.rdb.model.AdCampaignGroup;

import java.util.List;

/**
 * Created by Sisi on 15-2-28.
 */
@Service
public class AdCampaignRdbServiceImpl extends DaoTemplate implements AdCampaignRdbService {

    @Override
    public Long save(final AdCampaignGroup a) {
        return saveResultId(a);
//        String sql = "INSERT INTO ad_campaign_group " +
//                "(campaign_group_id, time_zone, fb_account_id, publish_batch_id, publish_batch_name," +
//                " product_conf_id, fb_connection_object_id, connection_object_name, configuration, name," +
//                " start_time, stop_time, start_utc_time, stop_utc_time, campaign_group_status," +
//                " updated_time, created_time, updated_utc_time, created_utc_time, objective,promoted_object,source_status,local_updated_time," +
//                " buying_type, adlabels, can_use_spend_cap, topline_id, spend_cap" +
//                ") VALUES" +
//                " (?, ?, ?, ?, ?," +
//                " ?, ?, ?, ?, ?, " +
//                " ? ,?, ?, ?, ?, " +
//                " ?, ?, ?, ?, ?, " +
//                " ?, ?, ?, ?, ?, " +
//                " ?,?,?)";
//        return saveResultId(sql, new PstAssign() {
//            @Override
//            public void setParam(PreparedStatement ps) throws SQLException {
//                ps.setObject(1, a.getCampaignGroupId());
//                ps.setObject(2, a.getTimezone());
//                ps.setObject(3, a.getFbAccountId());
//                ps.setObject(4, a.getPublishBatchId());
//                ps.setObject(5, a.getPublishBatchName());
//                ps.setObject(6, a.getProductConfId());
//                ps.setObject(7, a.getFbConnectionObjectId());
//                ps.setObject(8, a.getConnectionObjectName());
//                ps.setObject(9, a.getConfiguration());
//                ps.setObject(10, a.getName());
//                ps.setObject(11, a.getStartTime());
//                ps.setObject(12, a.getStopTime());
//                ps.setObject(13, a.getStartUtcTime());
//                ps.setObject(14, a.getStopUtcTime());
//                ps.setObject(15, a.getCampaignGroupStatus());
//                ps.setObject(16, a.getUpdatedTime());
//                ps.setObject(17, a.getCreatedTime());
//                ps.setObject(18, a.getUpdatedUtcTime());
//                ps.setObject(19, a.getCreatedUtcTime());
//                ps.setObject(20, a.getObjective());
//                ps.setObject(21, a.getPromotedObject());
//                ps.setObject(22, a.getSourceStatus());
//                ps.setObject(23, System.currentTimeMillis());
//                ps.setObject(24, a.getBuying_type());
//                ps.setObject(25, a.getAdlabels());
//                ps.setObject(26, a.getCan_use_spend_cap());
//                ps.setObject(27, a.getTopline_id());
//                ps.setObject(28, a.getSpend_cap());
//            }
//        });
    }


    @Override
    public AdCampaignGroup findByCampaignGroupId(Long campaignGroupId) {
        String sql = "select * from ad_campaign_group where campaign_group_id = ?";
        return findOne(sql, new Object[]{campaignGroupId}, DefaultRowMapper.autoRowMapper(AdCampaignGroup.class));
    }


    @Override
    public List<Long> findByFbAccountIdToPause(Long fbAccountId) {
        String sql = "select campaign_group_id from ad_campaign_group where fb_account_id =? ";
        return getJdbcTemplate().queryForList(sql, new Object[]{fbAccountId}, Long.class);
    }

    @Override
    public  List<Long> findByFbConnectObjectId(Long fbConnObjId) {
        String sql = "select campaign_group_id from ad_campaign_group where fb_connection_object_id =? ";
        return getJdbcTemplate().queryForList(sql, new Object[]{fbConnObjId}, Long.class);
    }

    @Override
    public List<Long> findByFbAccountIdAndTime(Long fbAccountId, String beginTime, String endTime) {
        String sql = "select campaign_group_id from ad_campaign_group where fb_account_id =? and created_time >= ? and created_time <= ?";
        return getJdbcTemplate().queryForList(sql, new Object[]{fbAccountId, beginTime, endTime}, Long.class);
    }

    @Override
    public  List<Long> findByFbConnectObjectIdAndTime(Long fbConnObjId, String beginTime, String endTime) {
        String sql = "select campaign_group_id from ad_campaign_group where fb_connection_object_id =? and created_time >= ? and created_time <= ?";
        return getJdbcTemplate().queryForList(sql, new Object[]{fbConnObjId, beginTime, endTime}, Long.class);
    }

    @Override
    public void updateEcTemplateMaterielId(Long campaignId, Long ecTemplateMaterielId) {
        String sql = "UPDATE ad_campaign_group SET ec_template_materiel_id = ? WHERE campaign_group_id=?";
        getJdbcTemplate().update(sql, new Object[]{ecTemplateMaterielId, campaignId});
    }

}
